import { reactive } from 'vue'


 const validateUsername = (rule, value, callback) => {

    //用户名正则，4到16位（字母，数字，下划线，减号）
    let reg = /^[a-zA-Z0-9_-]{3,16}$/
    if (reg.test(value)) {
        callback()
    } else {
        callback(new Error('请输入由字母,数字,下划线,减号组成的3到16位'))
    }
}
 const validatePassword = (rule, value, callback) => {
    let reg = /^[a-zA-Z0-9_-]{3,16}$/
    if (reg.test(value)) {
        callback()
    } else {
        callback(new Error('密码由字母,数字,下划线,减号组成的3到16位'))
    }
}
 const validateCaptcha = (rule, value, callback) => {
    let reg = /^[a-zA-Z0-9]{5}$/
    if (reg.test(value)) {
        callback()
    } else {
        callback(new Error('验证码只能是5位的数字或者字母'))
    }
}
export const useLoginState =(validateSuccess, validateDefeat)=> (formEl) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            if (validateSuccess) {
                validateSuccess()
            }
        } else {
            validateDefeat()
            return false
        }
    })
}

export const rules = reactive({
    userName: [{ validator: validateUsername, trigger: 'blur' }],
    passWord: [{ validator: validatePassword, trigger: 'blur' }],
    validateCaptcha: [{ validator: validateCaptcha, trigger: 'blur' }],
})

